package com.tentact.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tentact.mapper.PageUtil;
import com.tentact.service.MeetingService;
import com.tentact.vo.Employee;
import com.tentact.vo.Meeting;

@Controller
public class MeetingController {
	@Autowired
	private MeetingService service;
	
	//预定会议
	@RequestMapping("/orderMeeting")
	public String orderMeeting(Meeting meeting,Integer[]employeeids,HttpServletRequest request) {
		Employee employee = (Employee) request.getSession().getAttribute("emp");
		meeting.setReservationistid(employee.getEmployeeid());
		meeting.setReservationtime(new Date());
		meeting.setMeetingstatus(1);
		service.insertSelective(meeting, employeeids);
		return "redirect:selectMeetingPage";
	}
	//分页搜索会议
	@RequestMapping("/selectMeetingPage")
	public String selectMeetingPage(Meeting meeting,@DateTimeFormat(pattern="yyyy-MM-dd")Date reservationtime2,
			@DateTimeFormat(pattern="yyyy-MM-dd")Date begintime2,Integer currentpage,Model model) {
		
		if (currentpage == null) {
			currentpage = 1;
		}
		int recordCount = service.selectMeetingCountPage(meeting, reservationtime2, begintime2);
		PageUtil page = new PageUtil(10, recordCount, currentpage);
		List<Meeting> meetings =service.selectMeetingPage(meeting, reservationtime2, begintime2, page);
		/*meetings.forEach(meet->{
			System.out.println(meet.getEmployee().getEmployeename());
		});*/
		model.addAttribute("meetings", meetings);
		model.addAttribute("page", page);
		System.out.println(recordCount);
		System.out.println(meetings.size());
		return "searchmeetings";
	}
	@RequestMapping("/meetingdetails")
	public String meetingdetails(Integer meetingid,Model model) {
		Meeting meeting = service.selectByMeetingId(meetingid);
		model.addAttribute("meeting",meeting);
		return "meetingdetails";
	}
	@RequestMapping("/myOrderMeeting")
	public String myOrderMeeting(HttpServletRequest request,Model model) {
		Employee employee = (Employee) request.getSession().getAttribute("emp");
		List<Meeting> meetings = service.selectByReservationistId(employee.getEmployeeid());
		model.addAttribute("meetings", meetings);
		return "mybookings";
		
	}
	//mymeetingdetails
	@RequestMapping("/mymeetingdetails")
	public String mymeetingdetails(Integer meetingid,Model model) {
		Meeting meeting = service.selectByMeetingId(meetingid);
		model.addAttribute("meeting",meeting);
		return "mymeetingdetails";
	}
	//撤销会议
	@RequestMapping("/cancelMeeting")
	public String cancelMeeting(Meeting meeting) {
		meeting.setCanceledtime(new Date());
		meeting.setMeetingstatus(2);
		service.updateSelective(meeting);
		return "redirect:myOrderMeeting";
	}
	

}
